<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class m_pasien_kunjungan extends CI_Model {
	var $table = "kunjungan";

	public function __construct(){
		parent::__construct();
	}

	public function get_all(){
		$sql = " SELECT * FROM $this->table k 
		JOIN pasien p ON k.KdPasien = p.KdPasien
					ORDER BY KdKunjungan DESC";

		return $sql;
	}

	public function search($input){
		$filter = $input['filter'];
		$keyword = $input['keyword'];

		if($filter == 'by_code'){
			if(is_numeric($keyword)){
				$sql = "SELECT * FROM $this->table k 
				JOIN pasien p ON k.KdPasien = p.KdPasien 
				WHERE KdKunjungan = $keyword
				ORDER BY KdKunjungan DESC";
			}else{
				set_flash('danger','Format keyword salah');
				redirect('pasien_kunjungan', 'refresh');
			}
		}else if($filter == 'by_name'){
			$sql = "SELECT * FROM $this->table k 
				JOIN pasien p ON k.KdPasien = p.KdPasien 
				WHERE Nama LIKE '%$keyword%'
				ORDER BY KdKunjungan DESC";
		}else if($filter == 'by_keluhan'){
			$sql = "SELECT * FROM $this->table k 
				JOIN pasien p ON k.KdPasien = p.KdPasien 
				WHERE Keluhan LIKE '%$keyword%'
				ORDER BY KdKunjungan DESC";
		}else if($filter == "by_waktu"){
			$sql = "SELECT * FROM $this->table k 
				JOIN pasien p ON k.KdPasien = p.KdPasien 
				WHERE Tanggal BETWEEN '".$keyword[0]."' AND '".$keyword[1]."'
				ORDER BY KdKunjungan DESC";
		}
		return $sql;
	}

	public function store($input){
		$data = array(
			'KdPasien'	=> $input['pasien'],
			'Tanggal'	=> date('Y-m-d'),
			'TD'	=> $input['td'],
			'Suhu'	=> $input['suhu'],
			'RR'	=> $input['rr'],
			'HR'	=> $input['hr'],
			'Keluhan'	=> $input['keluhan']
			);
		$do_save = $this->db->insert($this->table, $data);
		if($do_save){
			set_flash('success','Data pasien kunjungan berhasil ditambahkan');
			return true;
		}else{
			set_flash('danger','Data pasien kunjungan gagal ditambahkan');
			return false;
		}
	}

	public function get_by_id($id){
		$this->db->where('KdKunjungan', $id);
		$query = $this->db->get($this->table);

		if($query->num_rows() > 0){
			$result = $query->row_array();
			$query->free_result();
		}else{
			$result = array();
		}

		return $result;
	}

	public function update($input){
		$data = array(
			'KdPasien'	=> $input['pasien'],
			'TD'	=> $input['td'],
			'Suhu'	=> $input['suhu'],
			'RR'	=> $input['rr'],
			'HR'	=> $input['hr'],
			'Keluhan'	=> $input['keluhan']
			);
		$this->db->where('KdKunjungan', $input['KdKunjungan']);
		$do_update = $this->db->update($this->table, $data);
		if($do_update){
			set_flash('success','Data pasien kunjungan berhasil diperbaharui');
			return true;
		}else{
			set_flash('danger','Data pasien kunjungan gagal diperbaharui');
			return false;
		}
	}

	public function destroy($id){
		$this->db->where('KdKunjungan', $id);
		$do_delete = $this->db->delete($this->table);

		if($do_delete){
			set_flash('success','Data pasien kunjungan berhasil dihapus');
			return true;
		}else{
			set_flash('danger','Data pasien kunjungan gagal dihapus');
			return false;
		}
	}

	public function export(){
		$this->db->join('pasien', 'pasien.KdPasien = kunjungan.KdPasien', 'left');
		$query = $this->db->get($this->table);
		if ($query->num_rows() > 0) {
			$result = $query->result_array();
			$query->free_result();
		} else {
			$result = array();
		}
		return $result;
	}

	public function export_by($filter, $keyword){
		if($filter == 'by_code'){
			if(is_numeric($keyword)){
				$this->db->join('pasien', 'pasien.KdPasien = kunjungan.KdPasien');
				$this->db->where('KdKunjungan', $keyword);
				$this->db->order_by('KdKunjungan', 'desc');
				$query = $this->db->get('kunjungan');				

				if($query->num_rows() > 0) {
					$result = $query->result_array();
					$query->free_result();
				}else{
					$result = array();
				}
				return $result;
			}else{
				set_flash('danger','Format keyword salah');
				redirect('pasien_kunjungan', 'refresh');
			}
		}else if($filter == 'by_name'){
			$this->db->join('pasien', 'pasien.KdPasien = kunjungan.KdPasien');
			$where = "Nama LIKE '%$keyword%'";
			$this->db->where($where);
			$this->db->order_by('KdKunjungan', 'desc');
			$query = $this->db->get('kunjungan');

			if($query->num_rows() > 0) {
				$result = $query->result_array();
				$query->free_result();
			}else{
				$result = array();
			}
			return $result;
		}else if($filter == 'by_keluhan'){
			$this->db->join('pasien', 'pasien.KdPasien = kunjungan.KdPasien');
			$where = "Keluhan LIKE '%$keyword%'";
			$this->db->where($where);
			$this->db->order_by('KdKunjungan', 'desc');
			$query = $this->db->get('kunjungan');

			if($query->num_rows() > 0) {
				$result = $query->result_array();
				$query->free_result();
			}else{
				$result = array();
			}
			return $result;

			$sql = "SELECT * FROM $this->table k
			JOIN pasien p ON k.KdPasien = p.KdPasien
			WHERE Informasi LIKE '%$keyword%'
			ORDER BY KdKunjungan DESC
			";
		}
	}
}

/* End of file m_pasien_kunjungan.php */
/* Location: ./application/models/m_pasien_data.php */